package com.chenfy.everysound.widget

import android.util.Log
import android.view.GestureDetector
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import androidx.core.view.GestureDetectorCompat
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.SimpleOnItemTouchListener

/**
 * Created by 陈丰尧 on 01/11/21
 */
class OnRecyclerItemClickListener : SimpleOnItemTouchListener() {
    private var lastPressVH: RecyclerView.ViewHolder? = null

    private val mGestureDetector: GestureDetectorCompat
            by lazy {
                GestureDetectorCompat(recyclerView.context, ItemTouchHelperGestureListener())
            }
    private lateinit var recyclerView: RecyclerView


    private var onClickListener: (vh: RecyclerView.ViewHolder, view: View) -> Boolean =
        { _, _ ->
            false
        }
    private var onLongClickListener: (vh: RecyclerView.ViewHolder, view: View) -> Boolean =
        { _, _ ->
            false
        }


    /**
     * 设置点击事件
     * @return true 表示消费这个事件,不会在向上传递
     *         false 表示不关心这个事件, 会继续调用父id(如果有id的话)
     */
    fun setOnClickListener(listener: (vh: RecyclerView.ViewHolder, view: View) -> Boolean): OnRecyclerItemClickListener {
        onClickListener = listener
        return this
    }

    /**
     * 设置长按事件
     * @return true 表示消费这个事件,不会在向上传递
     *         false 表示不关心这个事件, 会继续调用父id(如果有id的话)
     */
    fun setOnLongClickListener(listener: (vh: RecyclerView.ViewHolder, view: View) -> Boolean): OnRecyclerItemClickListener {
        onLongClickListener = listener
        return this
    }


    override fun onInterceptTouchEvent(rv: RecyclerView, e: MotionEvent): Boolean {
        this.recyclerView = rv
        when (e.action) {
            MotionEvent.ACTION_DOWN -> {
                val vh = getVH(rv, e)
                vh?.let {
                    it.itemView.isPressed = true
                    Log.d("Chen","设置press")
                    lastPressVH = vh
                }
            }
            MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {

                lastPressVH?.let {
                    it.itemView.isPressed = false
                }
            }
        }
        return mGestureDetector.onTouchEvent(e)
    }

    override fun onTouchEvent(rv: RecyclerView, e: MotionEvent) {
        this.recyclerView = rv
        mGestureDetector.onTouchEvent(e)

        when (e.action) {
            MotionEvent.ACTION_DOWN -> {
                val vh = getVH(rv, e)
                vh?.let {
                    vh.itemView.isPressed = true
                }
            }
            MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
                val vh = getVH(rv, e)
                vh?.let {
                    vh.itemView.isPressed = false
                }
            }
        }
    }

    private fun getVH(recyclerView: RecyclerView, e: MotionEvent): RecyclerView.ViewHolder? {
        val child = recyclerView.findChildViewUnder(e.x, e.y)
        var vh: RecyclerView.ViewHolder? = null
        return if (child != null) {
            recyclerView.getChildViewHolder(child)
        } else {
            null
        }
    }

    private inner class ItemTouchHelperGestureListener : GestureDetector.SimpleOnGestureListener() {
        override fun onSingleTapUp(e: MotionEvent): Boolean {
            val child = recyclerView.findChildViewUnder(e.x, e.y)
            var vh: RecyclerView.ViewHolder? = null
            vh = if (child != null) {
                recyclerView.getChildViewHolder(child)
            } else {
                return false
            }
            if (vh?.itemView?.isEnabled == false) {
                // 如果itemView是禁用的状态
                // 则不去处理点击事件
                return false
            }
            val clickViews = mutableListOf<View>()
            clickViewId(child, e.x, e.y, clickViews)
            for (clickView in clickViews) {
                val result = onClickListener(vh, clickView)
                // 如果result == true, 证明这个view消费了事件
                // 则事件不再进行传递
                if (result) {
                    return true
                }
            }
            // 最后调用onItemClick的事件
            return onClickListener(vh, vh.itemView)
        }

        override fun onLongPress(e: MotionEvent) {
            val child = recyclerView.findChildViewUnder(e.x, e.y)
            var vh: RecyclerView.ViewHolder? = null
            vh = if (child != null) {
                recyclerView.getChildViewHolder(child)
            } else {
                return
            }
            if (vh?.itemView?.isEnabled == false) {
                // 如果itemView是禁用的状态
                // 则不去处理点击事件
                return
            }
            val clickViews = mutableListOf<View>()
            clickViewId(child, e.x, e.y, clickViews)
            for (clickView in clickViews) {
                val result = onLongClickListener(vh, clickView)
                // 如果result == true, 证明这个view消费了事件
                // 则事件不再进行传递
                if (result) {
                    return
                }
            }
            // 最后调用ItemView的onItemClick的事件
            onLongClickListener(vh, vh.itemView)
        }

        private fun clickViewId(view: View, x: Float, y: Float, clickViews: MutableList<View>) {
            if (!view.isEnabled) {
                // 如果这个View/ViewGroup没有被启用, 则不响应点击事件了
                return
            }
            if (view is ViewGroup) {
                // 处理子View
                val childCount = view.childCount
                for (i in childCount - 1 downTo 0) {
                    val childView = view.getChildAt(i) ?: continue
                    val id = clickViewId(childView, x - view.left, y - view.top, clickViews)
                }

            }
            // 处理自己
            if (x >= view.left && x <= view.right && y >= view.top && y <= view.bottom) {
                // 将自己添加进去
                clickViews.add(view)
            }
        }
    }


}

// recyclerView 点击事件
fun RecyclerView.doOnItemClick(listener: (vh: RecyclerView.ViewHolder, view: View) -> Boolean){
    addOnItemTouchListener(OnRecyclerItemClickListener().setOnClickListener(listener))
}